package SSM.Chat.Listener;

import SSM.Chat.Dao.BookDao;
import SSM.Chat.Domain.AuthCode;
import SSM.Chat.Domain.Chat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.Date;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;


//@WebListener // 将该类配置为监听器  代替web.xml
// 删除聊天双方都已经删除的聊天信息
@Component
public class ChatMailBox {

    @Autowired
    private BookDao bookDao;

    @PostConstruct
    public void contextInitialized() {
        try {
            Timer timer = new Timer();
            timer.schedule(new TimerTask() {
                @Override
                public void run() {
                    // 获取主邮箱和次要邮箱都为2的状态
                    Chat chat = new Chat();
                    chat.setChatMailBoxOthersType(2);
                    chat.setChatMailBoxType(2);
                    List<Chat> chats = bookDao.ChatSelectMailBoxType(chat);
                    if(chats.size()>0){
                        for (int i = 0; i < chats.size(); i++) {
                            if(chats.get(i).getChatType().equals(
                                    "Photo")){
                                bookDao.ImageDelete(chats.get(i).getChatText());
                            }
                            bookDao.ChatDelete(chats.get(i).getChatId());
                        }
                    }


                }
            }, 0, 1000);

        } catch (Exception e) {
//                   LOGGER.error("GreyClientInitListener error", e);
            System.out.println(e);
        }

    }
}
